/**
 * 国际化系统
 * 
 * 提供多语言支持
 */

import { computed } from 'vue'
import { useSettings } from '../stores/settings'

// 文本资源类型
export interface TextResources {
  [key: string]: string | TextResources
}

// 中文文本资源
const zhCN: TextResources = {
  common: {
    confirm: '确认',
    cancel: '取消',
    save: '保存',
    delete: '删除',
    edit: '编辑',
    add: '添加',
    close: '关闭',
    minimize: '最小化',
    maximize: '最大化',
    restore: '还原',
    settings: '设置',
    refresh: '刷新',
    clear: '清空',
    test: '测试',
    send: '发送',
    receive: '接收',
    loading: '加载中...',
    success: '成功',
    error: '错误',
    warning: '警告',
    info: '信息'
  },
  
  navigation: {
    home: '首页',
    fileManager: '文件管理',
    ipcDemo: 'IPC通信',
    communication: '通信实战',
    multiWindow: '多窗口演示',
    broadcastDebug: '广播调试',
    settings: '设置',
    childWindow: '子窗口'
  },
  
  settings: {
    title: '应用设置',
    theme: {
      title: '主题设置',
      light: '浅色主题',
      dark: '深色主题',
      auto: '跟随系统',
      description: '选择您喜欢的界面主题'
    },
    language: {
      title: '语言设置',
      'zh-CN': '简体中文',
      'en-US': 'English',
      description: '选择界面显示语言'
    },
    general: {
      title: '通用设置',
      autoSave: '自动保存',
      notifications: '通知提醒',
      soundEffects: '音效',
      description: '配置应用的基本行为'
    }
  },
  
  titleBar: {
    addTab: '添加标签页',
    allPagesOpen: '所有页面都已打开',
    testClick: '测试点击'
  },
  
  fileManager: {
    title: '文件管理器',
    selectFile: '选择文件',
    selectFolder: '选择文件夹',
    openFile: '打开文件',
    saveFile: '保存文件',
    fileInfo: '文件信息',
    fileName: '文件名',
    fileSize: '文件大小',
    filePath: '文件路径',
    lastModified: '最后修改'
  },
  
  broadcast: {
    title: '广播通信',
    sendMessage: '发送消息',
    messageType: '消息类型',
    messageContent: '消息内容',
    chat: '聊天消息',
    notification: '通知消息', 
    status: '状态更新',
    alert: '警告消息',
    custom: '自定义消息',
    receivedMessages: '接收到的消息',
    noMessages: '还没有收到任何广播消息',
    clearMessages: '清空消息'
  },
  
  debug: {
    title: '调试信息',
    apiAvailability: 'API 可用性',
    listenerStatus: '监听器状态',
    messageFilter: '消息过滤器',
    windowInfo: '窗口信息',
    testApi: '测试API',
    resetListeners: '重置监听器',
    outputLogs: '输出日志',
    addTestMessage: '添加测试消息'
  }
}

// 英文文本资源
const enUS: TextResources = {
  common: {
    confirm: 'Confirm',
    cancel: 'Cancel',
    save: 'Save',
    delete: 'Delete',
    edit: 'Edit',
    add: 'Add',
    close: 'Close',
    minimize: 'Minimize',
    maximize: 'Maximize',
    restore: 'Restore',
    settings: 'Settings',
    refresh: 'Refresh',
    clear: 'Clear',
    test: 'Test',
    send: 'Send',
    receive: 'Receive',
    loading: 'Loading...',
    success: 'Success',
    error: 'Error',
    warning: 'Warning',
    info: 'Information'
  },
  
  navigation: {
    home: 'Home',
    fileManager: 'File Manager',
    ipcDemo: 'IPC Demo',
    communication: 'Communication',
    multiWindow: 'Multi-Window',
    broadcastDebug: 'Broadcast Debug',
    settings: 'Settings',
    childWindow: 'Child Window'
  },
  
  settings: {
    title: 'Application Settings',
    theme: {
      title: 'Theme Settings',
      light: 'Light Theme',
      dark: 'Dark Theme',
      auto: 'Follow System',
      description: 'Choose your preferred interface theme'
    },
    language: {
      title: 'Language Settings',
      'zh-CN': '简体中文',
      'en-US': 'English',
      description: 'Select interface display language'
    },
    general: {
      title: 'General Settings',
      autoSave: 'Auto Save',
      notifications: 'Notifications',
      soundEffects: 'Sound Effects',
      description: 'Configure basic application behavior'
    }
  },
  
  titleBar: {
    addTab: 'Add Tab',
    allPagesOpen: 'All pages are open',
    testClick: 'Test Click'
  },
  
  fileManager: {
    title: 'File Manager',
    selectFile: 'Select File',
    selectFolder: 'Select Folder',
    openFile: 'Open File',
    saveFile: 'Save File',
    fileInfo: 'File Information',
    fileName: 'File Name',
    fileSize: 'File Size',
    filePath: 'File Path',
    lastModified: 'Last Modified'
  },
  
  broadcast: {
    title: 'Broadcast Communication',
    sendMessage: 'Send Message',
    messageType: 'Message Type',
    messageContent: 'Message Content',
    chat: 'Chat Message',
    notification: 'Notification',
    status: 'Status Update',
    alert: 'Alert Message',
    custom: 'Custom Message',
    receivedMessages: 'Received Messages',
    noMessages: 'No broadcast messages received yet',
    clearMessages: 'Clear Messages'
  },
  
  debug: {
    title: 'Debug Information',
    apiAvailability: 'API Availability',
    listenerStatus: 'Listener Status',
    messageFilter: 'Message Filter',
    windowInfo: 'Window Information',
    testApi: 'Test API',
    resetListeners: 'Reset Listeners',
    outputLogs: 'Output Logs',
    addTestMessage: 'Add Test Message'
  }
}

// 语言资源映射
const resources = {
  'zh-CN': zhCN,
  'en-US': enUS
}

// 获取嵌套的翻译文本
const getNestedText = (obj: TextResources, path: string[]): string => {
  let current: any = obj
  for (const key of path) {
    if (current && typeof current === 'object' && key in current) {
      current = current[key]
    } else {
      return path.join('.') // 返回路径作为后备文本
    }
  }
  return typeof current === 'string' ? current : path.join('.')
}

// 翻译函数
export const useI18n = () => {
  const { language } = useSettings()
  
  const t = (key: string): string => {
    const currentResources = resources[language.value] || resources['zh-CN']
    const keys = key.split('.')
    return getNestedText(currentResources, keys)
  }
  
  // 响应式翻译函数
  const $t = computed(() => t)
  
  return {
    t,
    $t: $t.value,
    language: computed(() => language.value)
  }
}

// 全局翻译函数
export const t = (key: string): string => {
  const { t } = useI18n()
  return t(key)
}

